//
//  DocumentViewController.m
//  Navigation
//
//  Created by Alexander Skogberg on 2010-04-07.
//  Copyright 2010 Chalmers tekniska högskola. All rights reserved.
//

#import "DocumentViewController.h"

@implementation DocumentViewController

@synthesize webView;
@synthesize documentURL;
@synthesize receivedData;
@synthesize titleFileName;
@synthesize loadingIndicator;

#pragma mark Initialization Methods

/* Deallocate stuff */
- (void)dealloc 
{
	[webView release];
	[documentURL release];
	[titleFileName release];
	[super dealloc];
}

/* The view is created */
- (void)viewDidLoad 
{	
	//Set navigation bar title.
	UILabel *titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)] autorelease];
	[titleView setBackgroundColor:[UIColor clearColor]];
	[titleView setTextColor:[UIColor whiteColor]];
	[titleView setTextAlignment:UITextAlignmentCenter];
	[titleView setText:[NSString stringWithFormat:@"%@", titleFileName]];
	self.navigationItem.titleView = titleView;
	
	//Set up the activity indicator.
	loadingIndicator = [[[NSBundle mainBundle]loadNibNamed:@"LoadingIndicator" owner:self options:nil]objectAtIndex:0];
	[loadingIndicator.loadingLabel setText:@"Laddar..."];
	[loadingIndicator.activityIndicator startAnimating];
	[self.view addSubview:loadingIndicator];
	
	//Create the request.
	NSURLRequest *theRequest = [NSURLRequest requestWithURL:
		[NSURL URLWithString:documentURL]
		cachePolicy:NSURLRequestUseProtocolCachePolicy
		timeoutInterval:60.0];
		
	//Create the connection with the request and start loading the data.
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	
	if (theConnection)
	{ 
		[webView loadRequest:theRequest];//Tell the web view to load the document.
	}
	else 
	{
		NSLog(@"EPIC FAIL!"); // Inform the developer that the connection failed.
	}
	[super viewDidLoad];
}

/* Handles iPhone screen tilting */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark -

#pragma mark Internet Connection Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    /*
		This method is called when the server has determined that it
		has enough information to create the NSURLResponse.
	
		It can be called multiple times, for example in the case of a
		redirect, so each time we reset the data.
	
		receivedData is an instance variable declared elsewhere.
	*/
	
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	//Release objects.
    [connection release];
    [receivedData release];
	
	//Inform user.
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	//Inform user.
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    
	//Release objects.
    [connection release];
    [receivedData release];
	
	[self.loadingIndicator.loadingLabel setText:@"Klar!"];
	[self.loadingIndicator.activityIndicator stopAnimating];
	[self.loadingIndicator removeFromSuperview];
}

#pragma mark -

@end